﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class frmDrugsHelper :BaseWinFrom
{
    TextBox txtBox;

    public frmDrugsHelper(TextBox HelpTextBox)
    {
        if (HelpTextBox == null)
        {
            this.Dispose();
        }
        else
        {
            InitializeComponent();
        }
        txtBox = HelpTextBox;
        txtBox.Click += new EventHandler(txtBox_Click);

        Drugs dr = new Drugs();
        DB db = DB.getInstance();
        DataTable dt = dr.List("1");


        int count = 0;
        int length = this.Width / (50 + 30);
        Control c;
        foreach (DataRow i in dt.Rows)
        {
            //int y = count / length;
            //int x = count - y * length;
            c = new Control();
            c.Text = "AutoCreated";
            this.Controls.Add(c);
            c.Controls.Add(new Label()
            {
                Text = string.Format("{0}({1})", i["DrugsName"].ToString(), i["Rate"].ToString()),
                Top = 0,
                Left = 0,
                //Left = x * 80,
                //Top = y * 40,
                Width = 90,
                Height = 30,
                TextAlign = ContentAlignment.MiddleLeft,
            });
            c.Controls.Add(new TextBox()
            {
                Name = i["DrugsName"].ToString(),
                Tag = i["Rate"],
                Top = 0,
                Left = 93,
                //Top = y * 40,
                //Left = x * 80 + 53,
                Width = 25,
                Height = 30,
            });
            //c.Left = x * 80;
            //c.Top = y * 40;
            c.Width = 120;
            c.Height = 40;
            count++;
        }
        c = new Control();
        c.Width = 80; c.Height = 40;
        Controls.Add(c);
        Button btOk = new Button();
        btOk.Text = "OK";
        btOk.Click += new EventHandler(btOk_Click);
        c.Controls.Add(btOk);
        OnResize(null);
    }

    void txtBox_Click(object sender, EventArgs e)
    {
        this.Show();
        this.Visible = true;
    }

    protected override void OnClosing(CancelEventArgs e)
    {
        this.Visible = false;
        e.Cancel = true;
        base.OnClosing(e);
    }

    void btOk_Click(object sender, EventArgs e)
    {
        StringBuilder sb = null;
        foreach (Control i in Controls)
        {
            if (i.Text != "AutoCreated")
            {
                continue;
            }
            Label l = null;
            TextBox t = null;

            //foreach (Control j in i.Controls)
            //{
            //    if (j.GetType() == typeof(TextBox))
            //    {
            //        t = j as TextBox;
            //    }
            //    else if (j.GetType() == typeof(Label))
            //    {
            //        l = j as Label;
            //    }
            //}
            //if (l != null && t != null && !string.IsNullOrEmpty(t.Text))
            //{
            //    if (sb == null)
            //        sb = new StringBuilder();
            //    else
            //        sb.Append(DiagnosisDrugs.TAGSplitPairsGrounp);
            //    sb.Append(l.Text).Append(DiagnosisDrugs.TAGSplitNameValue).Append(t.Text);
            //}
            foreach (Control j in i.Controls)
            {
                if (j.GetType() == typeof(TextBox))
                {
                	if(j.Text==null || j.Text.Length==0)
                		continue;
                    if (sb == null)
                    {
                        sb = new StringBuilder();
                    }
                    else
                    {
                        sb.Append(DiagnosisDrugs.TAGSplitPairsGrounp);
                    }
                    sb.Append(string.Format("{0}{1}{2}", j.Name, DiagnosisDrugs.TAGSplitNameValue, j.Text));
                }
            }
        }
        if (sb != null)
        {
            sb.Append(DiagnosisDrugs.TAGEND);
            txtBox.Text = sb.ToString();
        }
        else
        {
            txtBox.Text = null;
        }
        WinFromDisplay();
    }

    protected override void OnResize(EventArgs e)
    {
        int i = 0;
        int l = this.Width / (90 + 30);
        foreach (Control o in Controls)
        {
            if (!string.IsNullOrEmpty(o.Name))
            {
                continue;
            }
            int y = i / l;
            int x = i - y * l;
            o.Top = y * o.Height;
            o.Left = x * o.Width;
            i++;
        }
        base.OnResize(e);
    }

    void WinFromDisplay()
    {
        this.Visible = false;
        //txtBox.Click -= new EventHandler(txtBox_Click);
        //this.Dispose(true);
    }
}